// src/web/swagger.rs
// 简化的Swagger/OpenAPI文档支持模块

use warp::{Filter, Reply};

/// 构建Swagger UI路由
pub fn build_swagger_routes() -> impl Filter<Extract = impl Reply, Error = warp::Rejection> + Clone {
    // Swagger UI路由
    let swagger_ui = warp::path("swagger-ui")
        .and(warp::path::end())
        .and(warp::get())
        .map(|| {
            // 返回Swagger UI HTML页面
            let html = r#"
<!DOCTYPE html>
<html>
<head>
    <title>IndustryAgent API Documentation</title>
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css">
    <style>
        html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; }
        *, *:before, *:after { box-sizing: inherit; }
        body { margin: 0; background: #fafafa; }
        .swagger-ui .topbar { background-color: #2d5aa0; }
        .swagger-ui .topbar .download-url-wrapper .download-url-button { background: #2d5aa0; }
    </style>
</head>
<body>
    <div id="swagger-ui"></div>
    <script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js"></script>
    <script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-standalone-preset.js"></script>
    <script>
        window.onload = function() {
            const ui = SwaggerUIBundle({
                url: '/api-docs/openapi.json',
                dom_id: '#swagger-ui',
                deepLinking: true,
                presets: [
                    SwaggerUIBundle.presets.apis,
                    SwaggerUIStandalonePreset
                ],
                plugins: [
                    SwaggerUIBundle.plugins.DownloadUrl
                ],
                layout: "StandaloneLayout",
                defaultModelsExpandDepth: 2,
                defaultModelExpandDepth: 2,
                docExpansion: "list",
                tryItOutEnabled: true,
                displayRequestDuration: true,
                filter: true,
                showExtensions: true,
                showCommonExtensions: true
            });
        };
    </script>
</body>
</html>
            "#;
            warp::reply::with_header(warp::reply::html(html), "content-type", "text/html")
        });

    // OpenAPI JSON路由
    let openapi_json = warp::path!("api-docs" / "openapi.json")
        .and(warp::get())
        .map(|| {
            // 手动构建简化的OpenAPI文档
            let openapi_doc = serde_json::json!({
                "openapi": "3.0.0",
                "info": {
                    "title": "IndustryAgent API",
                    "description": "基于华为云工业物联平台的语义转换系统 - 提供数据源到OPC UA模型的智能语义转换",
                    "version": "1.0.0",
                    "contact": {
                        "name": "IndustryAgent Team",
                        "email": "industryagent@example.com"
                    },
                    "license": {
                        "name": "Apache-2.0",
                        "url": "https://www.apache.org/licenses/LICENSE-2.0"
                    }
                },
                "servers": [
                    {
                        "url": "http://localhost:3030",
                        "description": "本地开发服务器"
                    },
                    {
                        "url": "https://api.industryagent.com",
                        "description": "生产服务器"
                    }
                ],
                "tags": [
                    {
                        "name": "system",
                        "description": "系统信息API"
                    }
                ],
                "paths": {
                    "/health": {
                        "get": {
                            "tags": ["system"],
                            "summary": "健康检查",
                            "description": "检查API服务的运行状态",
                            "responses": {
                                "200": {
                                    "description": "服务正常",
                                    "content": {
                                        "application/json": {
                                            "schema": {
                                                "$ref": "#/components/schemas/HealthResponse"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "/version": {
                        "get": {
                            "tags": ["system"],
                            "summary": "获取版本信息",
                            "description": "获取API版本、OPC UA版本和构建信息",
                            "responses": {
                                "200": {
                                    "description": "成功获取版本信息",
                                    "content": {
                                        "application/json": {
                                            "schema": {
                                                "$ref": "#/components/schemas/VersionResponse"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                "components": {
                    "schemas": {
                        "HealthResponse": {
                            "type": "object",
                            "properties": {
                                "status": {
                                    "type": "string",
                                    "description": "服务状态"
                                },
                                "timestamp": {
                                    "type": "string",
                                    "format": "date-time",
                                    "description": "时间戳"
                                },
                                "service": {
                                    "type": "string",
                                    "description": "服务名称"
                                }
                            }
                        },
                        "VersionResponse": {
                            "type": "object",
                            "properties": {
                                "api_version": {
                                    "type": "string",
                                    "description": "API版本"
                                },
                                "opcua_version": {
                                    "type": "string",
                                    "description": "OPC UA版本"
                                },
                                "build_time": {
                                    "type": "string",
                                    "description": "构建时间"
                                },
                                "git_commit": {
                                    "type": "string",
                                    "description": "Git提交哈希"
                                },
                                "rust_version": {
                                    "type": "string",
                                    "description": "Rust版本"
                                }
                            }
                        }
                    }
                }
            });
            warp::reply::json(&openapi_doc)
        });

    swagger_ui.or(openapi_json)
}